float4x4 matWorldViewProj	: WorldViewProjection;	
float4x4 matViewProj	: ViewProjection;
float4x4 matWorldI			: WorldInverseTranspose;
float4x4 matWorld			: World;
float4 lightPosition0 : LightPosition0;
float4 lightDiffuse0 : LightDiffuse0;

float4 MaterialDiffuse: Diffuse;

float Thickness: FloatParameter0;


/******************************************************************************/
/*                                                                            */
/*  *********  *******  ****  ****  *********  ****  ****  *******   *******  */
/*  *********  ***       ***  ***   *********  ****  ****  ***  ***  ***      */
/*     ***     *******    **  **       ***     ****  ****  ***  ***  *******  */
/*     ***     *******     ****        ***     ****  ****  *** ***   *******  */
/*     ***     ***        ******       ***      ***  ***   *******   ***      */
/*     ***     *******  ****  ****     ***       ******    ***  ***  *******  */
/*                                                                            */
/******************************************************************************/
texture texture0: TEXTURE0;

 
sampler2D texSampler0 = sampler_state
{
	Texture	  = (texture0);
    MIPFILTER = LINEAR;
    MAGFILTER = LINEAR;
    MINFILTER = LINEAR;
	
	ADDRESSU = CLAMP;
	ADDRESSV =CLAMP;
};

//application to vertex structure
struct vin
{ 
    float3 position   : POSITION;
    float3 normal	  : NORMAL;
    float2 tex0       : TEXCOORD0;
};
 
//vertex to pixel shader structure
struct vout
{
    float4 position	   : POSITION;
    float2 tex0        : TEXCOORD0;
};

struct vout_outline
{
    float4 position	   : POSITION;
};
 
 

/******************************************************************************/
/*                                                                            */
/*   ***       ***  ********  ********   *********  ********  ***     ***     */
/*    ***     ***   ***       ***   ***  *********  ***        ***   ***      */
/*     ***   ***    ***       ***   ***     ***     ***         *******       */
/*      *** ***     ********  ***   ***     ***     ********     *****        */
/*       *****      ***       ********      ***     ***         *******       */
/*        ***       ***       ***  **       ***     ***        ***   ***      */
/*         *        ********  ***   ***     ***     ********  ***     ***     */
/*                                                                            */
/******************************************************************************/


//VERTEX SHADER
void vs( in vin IN, out vout OUT ) 
{
    //getting to position to object space
    OUT.position    = mul(float4(IN.position,1.0), matWorldViewProj);
 
    //getting the position of the vertex in the world
    float4 posWorld = mul(float4(IN.position,1.0), matWorld);
 
	float3 normal   = mul( IN.normal , matWorldI );
	
	float3 lightDir0 = normalize(lightPosition0.xyz - posWorld.xyz);
	
	//use the light direction & normal to determine the intensity
	OUT.tex0.xy = float2(1-saturate(dot(lightDir0,normal)),0);
	
}

void vs_outline( in vin IN, out vout_outline OUT )
{
	float3 normal = mul (IN.normal, matWorldI)*Thickness;
	float3 posWorld = mul(float4(IN.position,1.0),matWorld)+normal;
	OUT.position = mul(float4(posWorld,1.0),matViewProj);
}

 
/******************************************************************************/
/*                                                                            */
/*            ********  ***   ****     ****    ******    ***                  */
/*            **    **  ***    ****   ****     ******    ***                  */
/*            **    **  ***      *** ***       ***       ***                  */
/*            ******    ***       *****        ******    ***                  */
/*            **        ***      *** ***       ***       ***                  */
/*            **        ***     ****  ****     ******    *********            */
/*            **        ***    ****    ****    ******    *********            */
/*                                                                            */
/******************************************************************************/

//PIXEL SHADER
float4 ps( in vout IN ) : COLOR
{
	float4 lightColor0 = tex2D(texSampler0, IN.tex0);
	return MaterialDiffuse*lightColor0;	
	
}

float4 ps_black (in vout_outline IN ) : COLOR
{
	return float4(0,0,0,1);
}


 
technique ToonEffect
{
    pass p0
	{
		CullMode = CW;
		ZEnable=true;
		ZWriteEnable=false;
		AlphaBlendEnable = false;
		vertexshader = compile vs_3_0 vs_outline();
        pixelshader  = compile ps_3_0 ps_black();
	}
    pass p1
    {
        CullMode = CCW;
		ZEnable=true;
		ZWriteEnable=true;
		vertexshader = compile vs_3_0 vs();
        pixelshader  = compile ps_3_0 ps();
    }
	
	
}